import React, { Component } from "react";
import "./index.css";
export default class Item extends Component {
  state = {
    mouse: false,
  };
  handleMouse = (flag) => {
    return () => {
      this.setState({
        mouse: flag,
      });
    };
  };
  handleRemove = (id) => {
    return () => {
      const { removeTodo } = this.props;
      removeTodo(id);
    };
  };
  render() {
    const { id, description, done } = this.props;
    const { mouse } = this.state;
    return (
      <li onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)} style={{ backgroundColor: mouse ? "#ccc" : "white" }}>
        <label>
          <input type="checkbox" checked={done} onChange={this.handleChecked(id)} />
          <span>{description}</span>
        </label>
        <button onClick={this.handleRemove(id)} className="btn btn-danger" style={{ display: mouse ? "block" : "none" }}>
          删除
        </button>
      </li>
    );
  }
  handleChecked = (id) => {
    return (event) => {
      const { updateTodo } = this.props;
      updateTodo(id, event.target.checked);
    };
  };
}
